package cn.dceast.controller;

import cn.dceast.common.result.RtnData;
import cn.dceast.service.MessageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by root on 16-11-21.
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    Logger log = Logger.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

//    Gson gson = new Gson();
    /**
     * 新增留言页面
     * @return
     */
    @GetMapping("addMsg")
    public ModelAndView addMessage() {
        return new ModelAndView("/feedback/addMessage");
    }

    @PostMapping("saveMsg")
    @ResponseBody
    public RtnData saveMessage(@RequestBody Map paramMap){
        log.error("请求参数--->"+paramMap);
        if(paramMap == null || paramMap.isEmpty()){
            return RtnData.fail(null, "请求参数为空。");
        }
        String title = (String) paramMap.get("TITLE");
        log.error("主题--->"+title);
        if(title == null || "".equals(title.trim())){
            return RtnData.fail(null, "主题参数为空。");
        }
        String mobile = (String) paramMap.get("PHONE");
        log.error("电话--->"+mobile);
        if(mobile == null || "".equals(mobile.trim())){
            return RtnData.fail(null, "电话参数为空。");
        }
        String content = (String) paramMap.get("CONTENT");
        log.error("内容--->"+content);
        if(content == null || "".equals(content.trim())){
            return RtnData.fail(null, "内容参数为空。");
        }

        int code = messageService.addMsg(paramMap);
        if(code > 0){
            return RtnData.ok(null, "留言成功。");
        }
        else {
            return RtnData.fail(null, "留言失败。");
        }
    }
    /**
     * 留言列表页面
     * @return
     */
    @GetMapping("listMsg")
    public ModelAndView listMessage() {
        return new ModelAndView("/feedback/messageList");
    }

    /**
     * 留言列表查询
     * @return
     */
    @GetMapping("getMsgList")
    @ResponseBody
    public RtnData getMsgList() {
//        log.error("请求参数--->"+paramMap);
        List<Map<String, Object>> list = messageService.getMsgListByCondition(null);
        if(list != null){
            return RtnData.ok(list);
        }
        else {
            return RtnData.fail(null, "留言列表查询失败。");
        }
    }
}
